package blog.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import blog.entity.ResultJson;
import blog.entity.WangEditorResultJson;
import blog.util.Random;
import blog.util.VerifyCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("util")
public class UtilController {


    @RequestMapping("/verifyCodeImg")
    public void generateCodeImg(Integer w, Integer h, HttpSession httpSession, HttpServletResponse httpServletResponse)throws Exception{
        if(w == null) {
            w = 120;
        }
        if(h == null) {
            h = 50;
        }
        String code = VerifyCode.getCodes(4);
        httpSession.setAttribute("code", code.toLowerCase());
        httpServletResponse.setContentType("image/JPEG");
        VerifyCode.output(VerifyCode.getCodeImg(code,w,h),httpServletResponse.getOutputStream()); //将验证码图片响应给客户端
    }

    @RequestMapping("/checkImgCode")
    public ResultJson checkImgCode(String imgCode, HttpSession httpSession) throws Exception{
        String sessCode = (String) httpSession.getAttribute("code");
        System.out.println(sessCode);
        if(sessCode == null){
            return ResultJson.isFail("验证码不存在");
        }
        if(imgCode.toLowerCase().equals(sessCode)){
            return ResultJson.SUCCESS;
        }else {
            return ResultJson.CODE_IMG_ERROR;

        }
    }

    @RequestMapping("/sendMail")
    public ResultJson sendMail(String name, HttpSession httpSession) throws Exception{
//        AdminDTO adminDTO = adminService.selectByName(name);
//        String email = adminDTO.getEmail();
        String code = VerifyCode.getCodes(4);
        httpSession.setAttribute("emailCode", code.toLowerCase());
//        EmailVerifyCode.send(email, code);
        return ResultJson.SUCCESS;
    }

    @RequestMapping("/checkEmailCode")
    public ResultJson checkEmailCode(String emailCode, HttpSession httpSession) throws Exception{
        String sessEmailCode = (String) httpSession.getAttribute("emailCode");
        if(sessEmailCode == null){
            return ResultJson.SYSTEM_ILLEGAL;
        }
        if(emailCode.toLowerCase().equals(sessEmailCode)){
            return ResultJson.SUCCESS;
        }else {
            return ResultJson.CODE_EMAIL_ERROR;
        }
    }

    @RequestMapping("/upload")
    public WangEditorResultJson upload(@RequestParam("image") MultipartFile image, HttpServletRequest request) throws Exception {
        if (image.getSize() > 0) {
            String path=request.getRealPath("/upload");
            String fileName = new Random()
                    .setPrefix(SecurityContextHolder.getContext().getAuthentication().getName())
                    .setSuffix(image.getOriginalFilename())
                    .getRandomName();
            if (fileName.endsWith("jpg") || fileName.endsWith("png") || fileName.endsWith("jpeg")) {
                File file = new File(path, fileName);
                image.transferTo(file);
                List<String> imgs = new ArrayList<>();
                imgs.add("../upload/" + file.getName());
                return WangEditorResultJson.isSuccess(imgs);
            }
        }
        return WangEditorResultJson.isFail();
    }

    @RequestMapping("/uploadImage")
    public ResultJson uploadImage(MultipartFile file, HttpServletRequest request) throws Exception {
        if (file.getSize() > 0) {
            String path=request.getRealPath("/upload");
            String fileName = new Random()
                    .setPrefix(SecurityContextHolder.getContext().getAuthentication().getName())
                    .setSuffix(file.getOriginalFilename())
                    .getRandomName();
            if (fileName.endsWith("jpg") || fileName.endsWith("png") || fileName.endsWith("jpeg")) {
                File f = new File(path, fileName);
                file.transferTo(f);
                return ResultJson.isSuccess("../upload/" + f.getName());
            }
        }
        return ResultJson.FAIL;
    }

    @RequestMapping("/deleteFile")
    public ResultJson deleteFile(@RequestBody String filename, HttpServletRequest request) throws Exception{
        if(filename != null || filename != ""){
            String path=request.getRealPath("/upload");
            File file = new File(path + "/" + filename);
            if(!file.exists()) return ResultJson.FILE_NULL;
            file.delete();
            return ResultJson.SUCCESS;
        }else{
            return ResultJson.PATH_NULL;
        }
    }

}